import requests
from lxml import etree
import csv
def grt_html(url):
    head={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36'}
    try:
        r = requests.get(url=url,headers=head)
        r.encoding = r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as error:
        print(error)
def parser(html):
    doc = etree.HTML(html)
    out_list=[]
    for row in doc.xpath('//*[@id="colR"]/div/dl/dd/ul/li'):
        row_data=[
            row.xpath('a/@title')[0],
            row.xpath('a/@href')[0]
        ]
        out_list.append(row_data)
    return out_list
def save_csv(path,content):
    with open(path,'a+', newline='', encoding='utf-8')as f:
        csv_write = csv.writer(f)
        csv_write.writerows(content)
if __name__ == '__main__':
    for i in range(1,6):
        if i == 1:
            url = 'https://www.cqie.edu.cn/html/2/xydt/Index.shtml'
        else:
            url = f'https://www.cqie.edu.cn/html/2/xydt/List_{i}.shtml'
        yuanma = grt_html(url)
        shuju = parser(yuanma)
        path = 'd:\\85.csv'
        save_csv(path,shuju)